package com.example.recorder;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;


import org.jetbrains.annotations.NotNull;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AudioRecorder extends MediaRecorder {
    private final String TAG = AudioRecorder.class.getSimpleName();
    MediaRecorder recorder;
    String outputFile;

    public void startRecording() {
        try {
            recorder = new MediaRecorder();
            outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() +"FinalAudio.amr";
            recorder.setAudioSource(AudioSource.MIC);
            recorder.setOutputFormat(OutputFormat.AMR_WB);
            recorder.setAudioEncoder(AudioEncoder.AMR_WB);
            recorder.setAudioSamplingRate(16000);
            recorder.setOutputFile(outputFile);
            recorder.prepare();
            recorder.start();
            Log.d(TAG, "开始录制");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void stopRecording() {
        try {
            recorder.stop();
            recorder.reset();
            recorder.release();
            recorder = null;
            upload();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void readFile() {
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(outputFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

//    public void upload() {
//        String response = null;
//        AudioSender sender = new AudioSender(outputFile);
//        FutureTask<String> uploadTask = new FutureTask<String>(sender);
//        ExecutorService executorService = Executors.newFixedThreadPool(1);
//        executorService.execute(uploadTask);
//        while (true) {
//            if (uploadTask.isDone()) {
//                try {
//                    response = uploadTask.get();
//                    break;
//                } catch (InterruptedException | ExecutionException e) {
//                    e.printStackTrace();
//                    Log.e("Upload", "Exception", e.getCause());
//                }
//            }
//        }
//    }
public void upload() {
    File output = new File(outputFile);
    Log.d(TAG, "开始上传");
    HttpUtil.postFile("http:/10.0.2.2:8001/upload", output, new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            Log.d(TAG, e.toString());
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            Log.d(TAG, response.body().string());
        }
    });
}
}
